package net.itgoo.parkingpay.ui.edit;

import net.itgoo.parkingpay.manage.ParkingManage;
import net.itgoo.parkingpay.rest.model.ParkingCardModel;
import net.itgoo.parkingpay.vendor.mvp.ParkingMVPLoadDataCallback;

import java.util.List;

/**
 * Created by apple on 16/11/21.
 */
public class ParkingEditPresenter implements ParkingEditContract.Presenter {

    private final ParkingEditContract.View mView;
    private final ParkingEditContract.DataSource mRepository;

    public ParkingEditPresenter(ParkingEditContract.View view,
                                ParkingEditContract.DataSource repository) {
        mView = view;
        mRepository = repository;
        view.setPresenter(this);
    }

    @Override
    public void start() {

    }

    @Override
    public void stop() {
        mRepository.cancel();
    }

    @Override
    public void getData() {
        String userId = ParkingManage.getInstance(mView.getContext()).getUserId();
        mRepository.getPlates(userId, new ParkingMVPLoadDataCallback<List<String>>() {
            @Override
            public void onDataLoaded(List<String> data) {
                if (!mView.isActive()) {
                    return;
                }

                if (data.isEmpty()) {
                    mView.showEmpty();
                } else {
                    mView.showPlates(data);
                }
            }

            @Override
            public void onDataError(String error) {
                if (!mView.isActive()) {
                    return;
                }

                mView.showEmpty();
                mView.showPlateFailure(error);
            }
        });

        mView.showLoading();
    }

    @Override
    public void openPlateEdit(int position) {
        String plate = mRepository.item(position);
        mView.showPlateEdit(position, plate);
    }

    @Override
    public void savePlate(final int position, final String plate) {
        if (plate == null || plate.isEmpty()) {
            return;
        }

        String oldPlate = mRepository.item(position);
        String userId = ParkingManage.getInstance(mView.getContext()).getUserId();
        if (position >= 0) {
            mRepository.updatePlate(userId, oldPlate, plate, new ParkingMVPLoadDataCallback() {
                @Override
                public void onDataLoaded(Object data) {
                    if (!mView.isActive()) {
                        return;
                    }

                    getData();
                }

                @Override
                public void onDataError(String error) {
                    if (!mView.isActive()) {
                        return;
                    }

                    mView.showPlateFailure(error);
                }
            });
        } else {
            mRepository.addPlate(userId, plate, new ParkingMVPLoadDataCallback() {
                @Override
                public void onDataLoaded(Object data) {
                    if (!mView.isActive()) {
                        return;
                    }

                    getData();
                }

                @Override
                public void onDataError(String error) {
                    if (!mView.isActive()) {
                        return;
                    }

                    mView.showPlateFailure(error);
                }
            });
        }
    }

    @Override
    public void deletePlate(int position) {
        String plate = mRepository.item(position);
        if (plate == null) {
            return;
        }
        String userId = ParkingManage.getInstance(mView.getContext()).getUserId();
        mRepository.deletePlate(userId, plate, new ParkingMVPLoadDataCallback() {
            @Override
            public void onDataLoaded(Object data) {
                if (!mView.isActive()) {
                    return;
                }

                getData();
            }

            @Override
            public void onDataError(String error) {
                if (!mView.isActive()) {
                    return;
                }

                mView.showDeleteFailure(error);
            }
        });
    }

    @Override
    public void openItem(int position) {
        String plate = mRepository.item(position);
        if (plate == null) {
            return;
        }
        ParkingCardModel card = mRepository.card(plate);
        mView.showPlateInfo(plate, card);
    }
}
